package net.zjitc.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter(filterName = "GoodsFilter",urlPatterns = "/*")
public class GoodsFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        Object user = request.getSession().getAttribute("user");
        String requestURI = ((HttpServletRequest) req).getRequestURI();
        if (requestURI.contains("/login")||requestURI.contains("bootstrap-3.4.1-dist")||requestURI.contains("index.jsp")||requestURI.contains("/")) {
            chain.doFilter(req, resp);
        } else {
            if (user != null) {
                chain.doFilter(req, resp);
            } else {
                req.setAttribute("usre", "请先登录");
                RequestDispatcher dispatcher = req.getRequestDispatcher("login/login.jsp");
                dispatcher.forward(req, resp);
            }
        }
    }
    public void init(FilterConfig config) throws ServletException {

    }

}
